package com.imooc.controller;

import com.imooc.config.ProjectUrlConfig;
import com.imooc.constants.CookieConstant;
import com.imooc.constants.RedisConstant;
import com.imooc.dataobject.SellerInfo;
import com.imooc.enums.ResultEnum;
import com.imooc.service.SellerService;
import com.imooc.utils.CookieUtil;
import com.imooc.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;

/**
 * FileName: SellUserController.java
 * Description: 卖家端用户controller
 *
 * @author HuangRI(874668739 @ qq.com)
 * @version 1.0
 * @date 2021/1/4
 */
@Controller
@Slf4j
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,
                              HttpServletResponse response,
                              Map<String, Object> model) {
        // 1.openid去和数据库的做匹配
        SellerInfo sellerInfoByOpenid = this.sellerService.findSellerInfoByOpenid(openid);
        if (null == sellerInfoByOpenid) {
            log.error("【卖家端登录】登录失败，找不到用户，openid={}", openid);
            model.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            model.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error", model);
        }

        // 2.设置token至redis
        String token = UUID.randomUUID().toString();
        String key = String.format(RedisConstant.TOKEN_PREFIX, token);
        this.redisUtil.set(key, openid, RedisConstant.EXPIRE_TIME);
        log.info("【卖家端登录】设置token, key={}, value={}", key, openid);

        // 3.设置token至cookie
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.EXPIRE_TIME);
        return new ModelAndView("redirect:" + this.projectUrlConfig.getSell() + "/sell/seller/order/list");
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                       HttpServletResponse response,
                       Map<String, Object> model) {
        // 1.读取cookie
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (null != cookie) {
            // 2.移除redis token缓存
            this.redisUtil.delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));

            // 3.清除cookie
            CookieUtil.set(response, CookieConstant.TOKEN, null, 0);
        }

        // 4.跳转到登出成功界面
        model.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
        model.put("url", "/sell/seller/order/list");
        return new ModelAndView("/common/success", model);
    }
}
